﻿using System.Globalization;
using System.Web.Hosting;
using EPiServer.Web.Hosting;
using OpenWaves.ImageTransformations.Web;

namespace OpenWaves.ImageTransformations.EPiServer
{
    public class EPiVirtualPathFileProvider : VirtualPathFileProvider
    {
        public EPiVirtualPathFileProvider(VirtualPathProvider innerProvider) : base(innerProvider)
        {
        }

        protected override VirtualFileWrapper CreateVirtualFileWrapper(VirtualFile file, RelativeUrl url)
        {
            return new EPiVirtualFileWrapper(url, file, this.InnerProvider);
        }

        private class EPiVirtualFileWrapper : VirtualFileWrapper
        {
            public EPiVirtualFileWrapper(RelativeUrl url, VirtualFile file, VirtualPathProvider virtualPathProvider) : base(url, file, virtualPathProvider)
            {
            }

            public override string Hash
            {
                get
                {
                    var virtualFileEx = this.File as VirtualFileEx;
                    if (virtualFileEx != null)
                        return virtualFileEx.Changed.ToFileTime().ToString(CultureInfo.InvariantCulture);

                    return base.Hash;
                }
            }
        }
    }
}